/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
/*
 * generated by Xtext
 */
package de.thwildau.tm.moses.xabnf.scoping;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.SimpleScope;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;

import com.google.inject.Inject;

import de.thwildau.tm.moses.abnf.abnf.Rule;
import de.thwildau.tm.moses.xabnf.ResourceFinder;
import de.thwildau.tm.moses.xabnf.xabnf.Binding;

/**
 * This class contains custom scoping description.
 * 
 * see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping
 * on how and when to use it 
 *
 */
public class XabnfScopeProvider extends org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider {
    
//    private static Logger LOG = Logger.getLogger(XabnfScopeProvider.class);
    
    @Inject
    private ResourceFinder resourceFinder;
    
    IScope scope_Binding_rule(Binding binding, EReference ref) {
        List<IEObjectDescription> descriptions = new ArrayList<>();
        Resource abnfResource = resourceFinder.findAbnfResource(binding);
        if(abnfResource != null) {
            TreeIterator<EObject> allContents = abnfResource.getAllContents();
            Iterator<Rule> filtered = IteratorExtensions.filter(allContents, Rule.class);
            while (filtered.hasNext()) {
                Rule rule = filtered.next();
                descriptions.add(EObjectDescription.create(rule.getName(), rule));
            }
        }
        
        return new SimpleScope(descriptions);
    }
    

//    IScope scope_AttributeBinding_name(AttributeBinding binding, EReference ref) {
//        System.out.println("Creating Scope for AttBinding " + binding.getName());
//        List<IEObjectDescription> descriptions = new ArrayList<>();
//        
//        RuleBinding ruleBinding = (RuleBinding) binding.eContainer();
//        EList<AttributeBinding> attributeBindings = ruleBinding.getAttributeBindings();
//        for(AttributeBinding attBinding : attributeBindings) {
//            QualifiedName qn = QualifiedName.create(attBinding.getName());
//            descriptions.add(EObjectDescription.create(qn, attBinding));
//        }
//        
//        return new SimpleScope(descriptions);
//    }
    
    
}
